import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Transfers text to and from the phone.
 */
class TextTransfer extends LocalServer {

  private Gui gui;

  protected boolean showIP() {return true;}

  private final class Gui {

    private String textToSendStr;
    private JTextArea textReceived;
    
    private JTextArea textArea() {
      return new JTextArea(15,30);
    }
    
    private JPanel textPane(String title, JTextArea textArea) {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
      p.add(new JLabel(title));
      p.add(new JScrollPane(textArea));
      return p;
    }

    void show() {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new BorderLayout());
      f.getContentPane().setPreferredSize(new Dimension(400,600));
      textReceived     = textArea();
      final JTextArea textToSend       = textArea();
      final JPanel    textReceivedPane = textPane("Text received",textReceived);
      final JPanel    textToSendPane   = textPane("Text to send",textToSend);
      JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
					textReceivedPane,
					textToSendPane);
      JPanel south = new JPanel();
      JButton sendButton = new JButton(new AbstractAction("Send") {
	  public void actionPerformed(ActionEvent e) {
	    textToSendStr = textToSend.getText();
	  }
	});
      south.add(sendButton);
      f.getContentPane().add(split,BorderLayout.CENTER);
      f.getContentPane().add(south,BorderLayout.SOUTH);
      f.pack();
      f.setVisible(true);
    }
    
    String getTextToSend() {
      String res = textToSendStr;
      textToSendStr = null;
      return res;
    }
    
    void showReceivedText(String str) {
      String s = textReceived.getText();
      if (s == null) s = "";
      s += "\n[" + dateTime() + "]\n";
      s += str;
      s += "\n";
      textReceived.setText(s);
    }
  }

  protected void beforeWeStart(String[] args) {
    gui = new Gui();
    gui.show();
  }

  private void updateTextField(String str) {
    gui.showReceivedText(str);
  }

  private String getUserText() {
    return gui.getTextToSend();
  }

  /** Receives text from the phone. */
  private class ReceiveTextHandler extends AbstractHandler {
    public String handle(Map<String,String> args) {
      String text = getArg(args,"text");
      updateTextField(urlDecode(text));
      return OK;
    }
  }

  /** Sends text to the phone if it's avaiable. */
  private class GetTextHandler extends AbstractHandler {
    public String handle(Map<String,String> args) {
      String text = getUserText();
      if (!Util.isEmpty(text)) {
	return urlEncode(text);
      }
      return "";
    }
  }

  Handler[] getHandlers() {
    return new Handler[] {
      new ReceiveTextHandler(),
      new GetTextHandler(),
    };
  }

  public static void main(String args[]) {
    new TextTransfer().realMain(args);
  }
  
}
